﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using System.Linq.Expressions;
using BitPortal.Utils;

namespace BitPortal.Business
{
    public static partial class MenuBLL
    {
        /// <summary>
        /// Xoa truc tiep
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public static int Delete(int MenuID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Menu menu = bitEnt.Menus.SingleOrDefault(ent => ent.MenuID == MenuID);
            if (menu != null)
            {
                //xoa tat ca cac menu con
                List<Menu> allChilds = new List<Menu>();
                menu.GetAllChilds(allChilds);
                foreach (Menu ent in allChilds)
                {
                    bitEnt.Menus.DeleteObject(bitEnt.Menus.SingleOrDefault(ent2 => ent2.MenuID == ent.MenuID));
                }
                //xoa menu
                bitEnt.Menus.DeleteObject(menu);
                return bitEnt.SaveChanges();
            }
            return bitEnt.SaveChanges();
        }

        /// <summary>
        /// Lay ra danh sach tat ca menu con chau cua menu hien tai.
        /// </summary>
        /// <param name="parentItem"></param>
        /// <param name="childs"></param>
        public static void GetAllChilds(this Menu parentItem, List<Menu> childs)
        {
            if (parentItem == null)
            {
                foreach (Menu ac in GetAll().Where(ent => ent.ParentID == null).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }
            else
            {
                foreach (Menu ac in GetAll().Where(ent => ent.ParentID == parentItem.MenuID).ToList())
                {
                    childs.Add(ac);
                    GetAllChilds(ac, childs);
                }
            }

        }

        /// <summary>
        /// Lay danh sach nhung menu cha cua menu hien tai.
        /// </summary>
        /// <param name="childItem"></param>
        /// <returns></returns>
        public static List<Menu> GetParents(this Menu childItem)
        {
            List<Menu> listParents = new List<Menu>();

            while (childItem.ParentID != null)
            {
                childItem = GetAll().SingleOrDefault(ent => ent.MenuID == childItem.ParentID);
                if (childItem == null)
                {
                    break;
                }
                listParents.Add(childItem);
            }

            return listParents;
        }


        /// <summary>
        /// Lay ra danh sach menu con cua menu hien tai.
        /// </summary>
        /// <param name="parentItem"></param>
        /// <returns></returns>
        public static List<Menu> GetChilds(this Menu parentItem)
        {
            if (parentItem == null)
            {
                return GetAll().Where(ent => ent.ParentID == null).ToList();
            }
            return GetAll().Where(ent => ent.ParentID == parentItem.MenuID).ToList();
        }

        /// <summary>
        /// Lay ra danh sach tat ca du lieu
        /// </summary>
        /// <returns></returns>
        public static IQueryable<Menu> GetAll(int? position = null)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var list = from ent in bitEnt.Menus
                       where (position == null || ent.Position == position)
                       orderby ent.SortOrder descending, ent.MenuID ascending
                       select ent;
            return list;

        }

        /// <summary>
        /// Lay ra doi tuong theo ID
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public static Menu GetByID(int MenuID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Menus.SingleOrDefault(ac => ac.MenuID == MenuID);
        }

        public static int Insert(this Menu item)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            bitEnt.Menus.AddObject(item);
            return bitEnt.SaveChanges();
        }

    }
}